package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.example.mapper.SetmealDishMapper;
import org.example.model.setmeal.SetmealDish;
import org.example.service.SetmealDishService;
import org.example.service.UserBasicService;
import org.example.utils.UserIdThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 套餐菜品关系管理
 */
@Service
@Slf4j
@Transactional
public class SetmealDishServiceImpl extends ServiceImpl<SetmealDishMapper, SetmealDish> implements SetmealDishService {

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private UserBasicService userBasicService;

    /**
     * 新增套餐菜品关系
     *
     * @param setmealDish
     * @param setmealId
     */
    @Override
    public Boolean InsertSetmealDish(SetmealDish setmealDish, int setmealId) {
        //获取Token中的数据
        Integer userId = UserIdThreadLocalUtils.getUser();
        if (userId <= 0) return false;
        setmealDish.setSetmealId(setmealId);
        setmealDish.setCreateUser(userId);
        setmealDish.setCreateDate(LocalDateTime.now());
        setmealDish.setUpdateUser(userId);
        setmealDish.setUpdateDate(LocalDateTime.now());
        setmealDish.setIsDeleted(false);
        return setmealDishMapper.insert(setmealDish) > 0;
    }

    /**
     * 根据套餐id查询套餐菜品关系列表
     *
     * @param setmealId
     */
    @Override
    public List<SetmealDish> SelectSetmealDishBySetmealId(int setmealId) {
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::isIsDeleted,false)
                .eq(SetmealDish::getSetmealId,setmealId);
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(queryWrapper);
        return setmealDishes;
    }

    /**
     * 根据套餐id删除套餐菜品关系列表
     *
     * @param setmealId
     */
    @Override
    public Boolean DeleteSetmealDishBySetmealId(int setmealId) {
        //获取Token中的数据
        Integer userId = UserIdThreadLocalUtils.getUser();
        if (userId <= 0) return false;
        List<SetmealDish> setmealDishes = this.SelectSetmealDishBySetmealId(setmealId);
        if (setmealDishes == null || setmealDishes.size() <= 0) return true;
        setmealDishes =  setmealDishes.stream().map(item ->{
            item.setIsDeleted(true);
            item.setUpdateUser(userId);
            item.setUpdateDate(LocalDateTime.now());
            return item;
        }).collect(Collectors.toList());
        for (SetmealDish setmealDish : setmealDishes) {
            Boolean flag = setmealDishMapper.updateById(setmealDish) > 0;
            if(!flag) return false;
        }
        return setmealId > 0;
    }
}
